var WorkProcedureInfo = function (res, month) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (res, month) {
      if (moment().isSame(month, 'month')) {
        month = moment().valueOf();
      } else {
        month = moment(month).endOf('month').valueOf();
      }
      self.month = month;
      this.setWorkProList(res);
      this.setMachineProList(res);
    },

    setWorkProList: function (res) {
      var workProList = res.workProList;
      workProList.sort(function (obj1, obj2) {
        var value = SysUtil.sortCompare(obj1, obj2, 'workShopName');
        if (value !== 0) {
          return value;
        }
        return SysUtil.sortCompare(obj1, obj2, 'processName');
      });

      var workPros = [];
      for (var i = 0; i < workProList.length; i++) {
        var workPro = workProList[i];
        var dayCapacity = workPro.dayCapacity;
        var sumCapacity = _.sumBy(workPro.listDuty, 'capacity');
        var workDay = this.calWorkDay(workPro);

        var rate;
        if (workDay === 0 || dayCapacity === 0) {
          rate = 0;
        } else {
          rate = sumCapacity / (workDay * dayCapacity) * 100;
          rate = _.round(rate, 2);
        }

        var capacityObj = this.getCapacityObj(workPro.listDuty);
        workPros.push({
          workShopName: workPro.workShopName,
          workshopId: workPro.workShopId,
          processName: workPro.processName,
          dayCapacity: dayCapacity,
          turnover: workPro.turnover,
          sumCapacity: sumCapacity,
          rate: rate,
          workshopTurnover: workPro.workShopStartTurnover,
          capacityObj: capacityObj
        });
      }
      self.workProList = workPros;
      self.searchWorkProList = [];

      var workProDateList = [];
      if (workPros.length > 0) {
        var capacityObj = workPros[0].capacityObj;
        workProDateList = _.keys(capacityObj);
        workProDateList = _.orderBy(workProDateList, [function (value) {
          return Number(value);
        }], ['desc']);
      }
      self.workProDateList = workProDateList;
    },

    setMachineProList: function (res) {
      var machineProList = res.machineProList;
      machineProList.sort(function (obj1, obj2) {
        var value = SysUtil.sortCompare(obj1, obj2, 'workShopName');
        if (value !== 0) {
          return value;
        }
        return SysUtil.sortCompare(obj1, obj2, 'equipmentNo');
      });

      var machinePros = [];
      for (var i = 0; i < machineProList.length; i++) {
        var machinePro = machineProList[i];
        var capacityObj = this.getCapacityObj(machinePro.dayList);
        var monthCount = _.sumBy(machinePro.dayList, 'capacity');

        machinePros.push({
          workShopName: machinePro.workShopName,
          workshopId: machinePro.workShopId,
          equipmentNo: machinePro.equipmentNo,
          processName: machinePro.processName,
          dayRate: this.getUtilizationRate(machinePro, 'day'),
          monthRate: this.getUtilizationRate(machinePro, 'month'),
          yearRate: this.getUtilizationRate(machinePro, 'year'),
          monthCount: monthCount,
          yearCount: machinePro.yearCount,
          capacityObj: capacityObj
        });
      }
      self.machineProList = machinePros;
      self.searchMachineProList = [];

      var machineProDateList = [];
      if (machinePros.length > 0) {
        var capacityObj = machinePros[0].capacityObj;
        machineProDateList = _.keys(capacityObj);
        machineProDateList = _.orderBy(machineProDateList, [function (value) {
          return Number(value);
        }], ['desc']);
      }
      self.machineProDateList = machineProDateList;
    },

    calWorkDay: function (workPro) {
      var month = self.month;
      var nowDay = moment(month).get('date');
      var restType = workPro.restType;
      var restDay;

      switch (restType) {
        case 0:
          restDay = 0;
          break;
        case 1:
          restDay = 1;
          break;
        case 2:
          restDay = 1;
          if (nowDay > 15) {
            restDay += 1;
          }
          break;
        case 3:
          restDay = 1;
          if (nowDay > 10) {
            restDay += 1;
          }
          if (nowDay > 20) {
            restDay += 1;
          }
          break;
        case 4:
          restDay = this.getWeekCount([0]);
          break;
        case 5:
          var sunRest = this.getWeekCount([0]);
          var satRest = this.getWeekCount([6]);
          restDay = sunRest + satRest * 0.5;
          break;
        case 6:
          restDay = this.getWeekCount([6, 0]);
          break;
      }

      var workDay = nowDay - restDay;
      return workDay;
    },

    getWeekCount: function (weeks) {
      var month = self.month;
      var endMoment = moment(month);
      var restMoment = moment(month).startOf('month');

      var restDay = 0;
      while (restMoment.isSameOrBefore(endMoment)) {
        var week = restMoment.get('day');
        if (weeks.indexOf(week) !== -1) {
          restDay++;
        }
        restMoment.add(1, 'day');
      }
      return restDay;
    },

    getCapacityObj: function (capacityList) {
      var month = self.month;
      var day = moment(month).get('date');
      var capacityObj = {};
      for (var i = 0; i < day; i++) {
        var date = i + 1;
        capacityObj[date] = {
          date: date,
          capacity: 0
        };
      }

      if (capacityList === null) {
        capacityList = [];
      }
      for (var i = 0; i < capacityList.length; i++) {
        var capacity = capacityList[i];
        var delayTime = capacity.delayTime;
        if (SysUtil.isEmpty(delayTime)) {
          continue;
        }

        var date = moment(delayTime).get('date');
        capacityObj[date].capacity = capacity.capacity;
      }

      return capacityObj;
    },

    getUtilizationRate: function (machinePro, type) {
      var dailyWorkingHours = machinePro.dailyWorkingHours;
      var month = self.month;

      var time, workTime;
      switch (type) {
        case 'day':
          var dayTime = machinePro.dayTime;
          time = SysUtil.isEmpty(dayTime) ? 0 : dayTime;
          workTime = dailyWorkingHours;
          break;
        case 'month':
          var monthTime = machinePro.monthTime;
          time = SysUtil.isEmpty(monthTime) ? 0 : monthTime;
          workTime = moment(month).get('date') * dailyWorkingHours;
          break;
        case 'year':
          var yearTime = machinePro.yearTime;
          time = SysUtil.isEmpty(yearTime) ? 0 : yearTime;
          workTime = moment(month).get('dayOfYear') * dailyWorkingHours;
          break;
      }

      if (time === 0 || workTime === 0) {
        return 0;
      }

      var rate = time / workTime * 100;
      rate = _.round(rate, 2);
      return rate;
    }
  };

  this.getChartWorkProList = function (index) {
    var workProList = this.searchWorkProList;
    var workPros = workProList.slice(index);
    workPros.pushArray(workProList.slice(0, index));
    return workPros;
  };

  this.getChartWorkProDateList = function (index) {
    var workProDateList = this.workProDateList;
    var workProDates = workProDateList.slice(index);
    workProDates.pushArray(workProDateList.slice(0, index));
    return workProDates;
  };

  this.getChartMachineProList = function (index) {
    var machineProList = this.searchMachineProList;
    var machineProList = machineProList.slice(index);
    machineProList.pushArray(machineProList.slice(0, index));
    return machineProList;
  };

  this.getChartMachineProDateList = function (index) {
    var machineProDateList = this.machineProDateList;
    var machineProDates = machineProDateList.slice(index);
    machineProDates.pushArray(machineProDateList.slice(0, index));
    return machineProDates;
  };

  this.resetProData = function (res, month) {
    fn.initData(res, month);
  };

  this.getWorkshopList = function () {
    var workProList = this.workProList;
    var workshopObj = {};
    for (var i = 0; i < workProList.length; i++) {
      var workPro = workProList[i];
      var workshopId = workPro.workshopId;
      if (SysUtil.isEmpty(workshopId)) {
        continue;
      }
      if (workshopObj[workshopId] === undefined) {
        workshopObj[workshopId] = {
          id:workshopId,
          name:workPro.workShopName
        }
      }
    }
    var machineProList = this.machineProList;
    for (var i = 0; i < machineProList.length; i++) {
      var machinePro = machineProList[i];
      var workshopId = machinePro.workshopId;
      if (SysUtil.isEmpty(workshopId)) {
        continue;
      }
      if (workshopObj[workshopId] === undefined) {
        workshopObj[workshopId] = {
          id:workshopId,
          name:machinePro.workShopName
        }
      }
    }

    var workshopList = _.values(workshopObj);
    return workshopList;
  };

  this.toSearchData = function (search) {
    var workshopId = search.workshopId;
    var workProList = this.workProList;
    var machineProList = this.machineProList;
    if (workshopId === '') {
      workProList = _.clone(workProList);
      machineProList = _.clone(machineProList);
    } else {
      workProList = _.filter(workProList, {
        workshopId:workshopId
      });
      machineProList = _.filter(machineProList, {
        workshopId:workshopId
      });
    }
    this.searchWorkProList = workProList;
    this.searchMachineProList = machineProList;
  };

  fn.initData(res, month);
};
